package cn.wgx.common.base;

import cn.wgx.common.util.ResponseDataUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;


public abstract class IBaseController<S extends IBaseService, T> extends BaseLogger{

    protected T t;

    @SuppressWarnings("all")
    @Autowired
    protected S service;


    @ApiOperation(value = "根据实体参数查询列表,分页查询(page第几页, limit每页数量,默认20)")
    @GetMapping("list")
    public Map list(@ModelAttribute T t,
                    @RequestParam(required = false, defaultValue = "1") Integer page,
                    @RequestParam(required = false, defaultValue = "20") Integer limit) {
        Page page1 = new Page(page, limit);
        return ResponseDataUtil.returnData(page1, service.page(page1, new QueryWrapper<>(t)).getRecords());
    }

    @ApiOperation(value = "根据主键ID查询单条数据")
    @GetMapping("get")
    public Map getOne(@RequestParam Integer id) {
        return ResponseDataUtil.returnData(service.getById(id));
    }

    @ApiOperation(value = "新增实体数据")
    @PostMapping("add")
    public Map add(@Validated @RequestBody T t) {
        service.save(t);
        return ResponseDataUtil.returnData(t);
    }

    @ApiOperation(value = "新增实体数据-批量")
    @PostMapping("addBatch")
    public Map addBatch(@Validated @RequestBody List<T> t) {
        service.saveBatch(t);
        return ResponseDataUtil.returnData(t);
    }

    @ApiOperation(value = "根据主键ID修改实体数据,可局部修改")
    @PatchMapping("update")
    public Map update(@RequestBody T t) {
        return ResponseDataUtil.returnData(service.updateById(t));
    }

    @ApiOperation(value = "根据主键ID修改实体数据-批量,可局部修改")
    @PatchMapping("updateBatch")
    public Map updateBatch(@RequestBody List<T> t) {
        return ResponseDataUtil.returnData(service.updateBatchById(t));
    }

    @ApiOperation(value = "根据主键ID删除数据")
    @DeleteMapping("del/{id}")
    public Map del(@PathVariable Integer id) {
        return ResponseDataUtil.returnData(service.removeById(id));
    }

    @ApiOperation(value = "根据主键ID删除数据-批量")
    @DeleteMapping("delBatch")
    public Map delBatch(@RequestBody List<Integer> ids) {
        return ResponseDataUtil.returnData(service.removeByIds(ids));
    }

    /**
     * 默认根请求空间
     *
     * @param p
     * @return
     */
    @ApiIgnore()
    @ResponseBody
    @RequestMapping(value = "{p}", method = {RequestMethod.GET, RequestMethod.POST})
    public Map<String, Object> defaultUrl(@PathVariable String p, HttpServletRequest request, HttpSession httpSession) {
        Map<String, Object> map = ResponseDataUtil.getInitMap();
        if ("test1".equals(p)) {
            httpSession.setAttribute("testFlag", "1");
            map.put("res", "打开测试模式");
            map.put("success", true);
            Object o = httpSession.getAttribute("user");
        } else {
            return ResponseDataUtil.returnErrorMap("没找到接口.");
        }
        return map;
    }

}
